package uit.shoeshop.action;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import uit.shoeshop.dto.CategoryDTO;
import uit.shoeshop.lib.Util;
import uit.shoeshop.model.CategoryModel;

import com.opensymphony.xwork2.ActionSupport;
public class ManagecategoryAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private String link;
	private String sid;
	private String saction;
	private ArrayList<CategoryDTO> categoryList;
	private ArrayList<CategoryDTO> categoryParentList;
	private CategoryDTO oneCategory;
	private int err;
	private String title;
	private int parent_id;
	private int enabled;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
    public String execute() throws Exception {
    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
    	String ret = NONE;

    	CategoryModel category = new CategoryModel();
    	setLink(this.request.getRequestURL().toString());
    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST")&& getSaction() != null && getSid() !=null){
        		if(getSaction().equals("del") && Util.isIntNumber(getSid())){
        			int result = category.delCategory(Integer.parseInt(getSid()));
        			if( result == 1)
        				ret = SUCCESS;
        			else if(result == -1){
        				setErr(-1);
        				setCategoryList(category.getAllCategory());
        				ret = NONE;
        			}else if(result == -2){
        				setErr(-2);
        				setCategoryList(category.getAllCategory());
        				ret = NONE;
        			}else
        				ret = ERROR;
        		}else if(getSaction().equals("active") && Util.isIntNumber(getSid())){
        			int result = category.activeCategory(Integer.parseInt(getSid()));
        			if( result == 1)
        				ret = SUCCESS;
        			else
        				ret = ERROR;
        		}else{
        			ret= ERROR;
        		}
        	}else{     		
        		setCategoryList(category.getAllCategory());
        	}
    	}catch(Exception e){
    		ret= ERROR;
    	}finally{
    		category = null;
    	}     	
    	return ret;
    }

    public String edit() throws Exception {
    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
    	String ret = ERROR;
    	setLink("managecategory.html");
    	CategoryModel category = new CategoryModel();

    	try{
        	if(this.request.getMethod().equalsIgnoreCase("POST")&& getSaction() != null && getSid() !=null){
        		if(getSaction().equals("change") && Util.isIntNumber(getSid())){
        			setOneCategory(category.getCategorybyID(Integer.parseInt(getSid())));
        			setCategoryParentList(category.getAllParentCategory());
        			if(getOneCategory() != null && getOneCategory().getTitle()!= null)
        				ret= NONE;
        			else
        				ret=ERROR;
        		}else if(getSaction().equals("process") && Util.isIntNumber(getSid())){
        			int result = category.editCategory(Integer.parseInt(getSid()),getTitle(),getParent_id(),getEnabled());
        			if( result == 1)
        				ret = SUCCESS;
        			else if(result == -1){
        				setErr(-1);
        				setOneCategory(category.getCategorybyID(Integer.parseInt(getSid())));
            			setCategoryParentList(category.getAllParentCategory());
            			setSaction("change");
        				ret = NONE;
        			}else
        				ret = ERROR;
        		}else{
        			ret = ERROR;
        		}
        	}
    	}catch(Exception e){
    		ret= ERROR;
    	}finally{
    		category = null;
    	}     	
    	return ret;
    }    
    
    public String add() throws Exception {
    	if(Util.getLogin("login", request) == 0) response.sendRedirect("index.html");
    	String ret = NONE;
    	setLink("managecategory.html");
    	CategoryModel category = new CategoryModel();

    	try{
			setCategoryParentList(category.getAllParentCategory());
        	if(this.request.getMethod().equalsIgnoreCase("POST")&& getSaction() != null){
        		if(getSaction().equals("process")){
        			int result = category.addCategory(getTitle(),getParent_id(),getEnabled());
        			if( result == 1)
        				ret = SUCCESS;
        			else
        				ret = ERROR;
        		}else{
        			ret = ERROR;
        		}
        	}
    	}catch(Exception e){
    		ret= ERROR;
    	}finally{
    		category = null;
    	}     	
    	return ret;
    }
    
	public void setLink(String link) {
		this.link = link;
	}

	public String getLink() {
		return link;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getSid() {
		return sid;
	}

	public void setSaction(String saction) {
		this.saction = saction;
	}

	public String getSaction() {
		return saction;
	}

	public void setCategoryList(ArrayList<CategoryDTO> categoryList) {
		this.categoryList = categoryList;
	}

	public ArrayList<CategoryDTO> getCategoryList() {
		return categoryList;
	}

	public void setOneCategory(CategoryDTO oneCategory) {
		this.oneCategory = oneCategory;
	}

	public CategoryDTO getOneCategory() {
		return oneCategory;
	}

	public void setErr(int err) {
		this.err = err;
	}

	public int getErr() {
		return err;
	}

	public void setCategoryParentList(ArrayList<CategoryDTO> categoryParentList) {
		this.categoryParentList = categoryParentList;
	}

	public ArrayList<CategoryDTO> getCategoryParentList() {
		return categoryParentList;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setParent_id(int parent_id) {
		this.parent_id = parent_id;
	}

	public int getParent_id() {
		return parent_id;
	}

	public void setEnabled(int enabled) {
		this.enabled = enabled;
	}

	public int getEnabled() {
		return enabled;
	}	

}
